/*
 * bw_dly_main.cpp
 *
 *  Created on: 2018年9月6日
 *  Author: Degang.Wu (windragon@126.com)
 *  Desc:
 */

#include <net/if.h>
#include "public.h"
#include "bw_fcgi.h"
#include "bw_Str.h"
#include "bw_FcgiRequest.h"
#include "bw_FileHandler.h"

//#define LOC_TEST	//本地测试

using namespace bwcgi;

extern const char* bwenc_http_handle(FcgiRequest* pFcgiReq);		// http回调
extern const char* bwenc_http_imgup(FcgiRequest* pFcgiReq);			// 图片文件上传
extern const char* bwenc_http_logexport(FcgiRequest* pFcgiReq);		// 系统日志导出
extern const char* bwenc_http_cfgexport(FcgiRequest* pFcgiReq);		// 系统设置-配置导出
extern const char* bwenc_http_cfgimport(FcgiRequest* pFcgiReq);		// 系统设置-配置导入
extern const char* bwenc_http_upgrade(FcgiRequest* pFcgiReq);		// 升级包上传请求地址


/*
 * 服务启动初始化工作，如启动相关线程
 */
int init()
{
	enc_set_master(1);
	if(0 == shrmem_init(0))
	{
		cgi_debug("shrmem_init failed! try agian");
		sleep(3);
        if(0 == shrmem_init(0))
		    exit(0);
	}
	cgi_debug("share memory init ok!");

//	sys_network_init();
	
	// 图片清理
	//img_clean();

	return 0;
}

#ifdef LOC_TEST
static int g_shrmid = -1;
static void* g_shrmpt = NULL;

int local_test()
{
	enc_set_master(1);
	cgi_debug("local_test\n");

	g_shrmsem = sem_open(ENC_SHMINITSEM, O_CREAT, 0644, 1);
	if (g_shrmsem == SEM_FAILED)
	{
		cgi_debug("bwenc====>shrmsem_open failed!\n");
		return 0;
	}

	/*g_devsem = sem_open(ENC_GDEVSEM, O_CREAT, 0644, 1);
	if (g_devsem == SEM_FAILED)
	{
		cgi_debug("bwenc====>global devsem open failed!\n");
		shrmem_uninit();
		return 0;
	}

	g_logsem = sem_open(ENC_GLOGSEM, O_CREAT, 0644, 1);
	if (g_logsem == SEM_FAILED)
	{
		cgi_debug("bwenc====>global logsem open failed!\n");
		shrmem_uninit();
		return 0;
	}*/

	if (enc_is_master())
	{
		g_shrmid = shmget((key_t) (ENC_SHRMEMKEY_M), sizeof(stENC_DEVSET_MASTER),
				0666 | IPC_CREAT);
	}
	else
	{
		g_shrmid = shmget((key_t) (ENC_SHRMEMKEY_S), sizeof(stENC_DEVSET_SLAVE),
				0666 | IPC_CREAT);
	}
	if (g_shrmid < 0)
	{
		cgi_debug("bwenc====>can't create shared_mem!\n");
		shrmem_uninit();
		return 0;
	}

	g_shrmpt = shmat(g_shrmid, NULL, 0);
	if (g_shrmpt == (void *) (-1))
	{
		cgi_debug("bwenc====>can't use shared_mem!\n");
		shrmem_uninit();
		return 0;
	}

	if (enc_is_master())
	{
		g_encset_mst = (pENC_DEVSET_MASTER) g_shrmpt;
	}
	else
	{
		g_encset_slv = (pENC_DEVSET_SLAVE) g_shrmpt;
	}
	
	cgi_debug("share memory init ok!");
	
	return 0;
}
#endif

void mount_ramfs() {
	int ret = 0;
	char cmd[SZ_1K];
	snprintf(cmd, sizeof(cmd), "mkdir -p %s", BW_RAM_LOG_PATH);
	ret = system(cmd);
	fprintf(stderr, "cmd=%s, ret=%d\n", cmd, ret);

	snprintf(cmd, sizeof(cmd),
			"umount %s; mount -t ramfs none %s -o maxsize=12000",
			BW_RAM_LOG_PATH, BW_RAM_LOG_PATH);
	ret = system(cmd);
	fprintf(stderr, "cmd=%s, ret=%d\n", cmd, ret);
}

int main(int argc, char **argv) {
    if(argc > 1 && BWStr::equals(argv[1], "-version"))
    {
//	    fprintf(stdout, "v1.0.0");
        fprintf(stdout, "v2.0.0");
        return 0;
    }

	mount_ramfs();

	setDebug(1);	// 默认关闭，上线不应开启debug

#ifndef LOC_TEST
	init();
#else
	local_test();
#endif

	registeHttpHandle("/bwfcgi/encoder", bwenc_http_handle);
	registeHttpHandle("/bwfcgi/encimgup", bwenc_http_imgup);//osd图片
	registeHttpHandle("/bwfcgi/enclogexport", bwenc_http_logexport);
	registeHttpHandle("/bwfcgi/enccfgexport", bwenc_http_cfgexport);
	registeHttpHandle("/bwfcgi/enccfgimport", bwenc_http_cfgimport);
	registeHttpHandle("/bwfcgi/encupgrade", bwenc_http_upgrade);

	Fcgi::start();

	shrmem_uninit();	// could not run here!!!

	return 0;
}
